package service;

import entity.Cart;
import entity.Commodity;
import util.DBHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * 数据逻辑层
 */

public class CommodityLogic {


    // 根据商品编号获得商品资料
    public Commodity getCommodityById(int id) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = DBHelper.getConnection();
            String sql = "select * from commodity where id=?;";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1,id);//1代表第一个问号，id是这个位置的值
            rs = stmt.executeQuery();

            if (rs.next()) {
                Commodity commodity = new Commodity();
                commodity.setId(rs.getInt("id"));
                commodity.setName(rs.getString("name"));
                commodity.setPrice(rs.getDouble("price"));
                commodity.setNumber(rs.getInt("number"));
                commodity.setImg(rs.getString("img"));
                commodity.setAddress(rs.getString("address"));
                return commodity;
            }else{
                return null;
            }


        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        } finally {
            // 释放数据集对象
            if (rs != null) {
                try {
                    rs.close();
                    rs = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            //释放语句对象
            if (stmt != null) {
                try {
                    stmt.close();
                    stmt = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

        }



    }

    // 获得所有的商品信息
    public ArrayList<Commodity> getAllItems() {

        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        ArrayList<Commodity> list = new ArrayList<Commodity>();
        try {
            conn = DBHelper.getConnection();
            String sql = "select * from commodity";
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            while (rs.next()) {
                Commodity commodity = new Commodity();
                commodity.setId(rs.getInt("id"));
                commodity.setName(rs.getString("name"));
                commodity.setPrice(rs.getDouble("price"));
                commodity.setNumber(rs.getInt("number"));
                commodity.setImg(rs.getString("img"));
                commodity.setAddress(rs.getString("address"));
                list.add(commodity);//把一个商品加入到集合
            }
            return list;

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        } finally {
            // 释放数据集对象
            if (rs != null) {
                try {
                    rs.close();
                    rs = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            //释放语句对象
            if (stmt != null) {
                try {
                    stmt.close();
                    stmt = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

        }

    }

    //获取最近浏览的前五条商品信息
    public ArrayList<Commodity> getViewList(String list) {
        System.out.println("list:" + list);
        ArrayList<Commodity> cdList = new ArrayList<Commodity>();
        int iCount = 5; //每次返回前五条记录
        if (list != null && list.length() > 0) {
            String[] arr = list.split(",");
            System.out.println("arr.length=" + arr.length);
            //如果商品记录大于等于5条
            if (arr.length >= 5) {
                for (int i = arr.length - 1; i >= arr.length - iCount; i--) {
                    cdList.add(getCommodityById(Integer.parseInt(arr[i])));
                }
            } else {
                for (int i = arr.length - 1; i >= 0; i--) {
                    cdList.add(getCommodityById(Integer.parseInt(arr[i])));
                }
            }
            return cdList;
        } else {
            return null;
        }
    }


}
